/** by daoyu 2011-9-23*/

function upload_basket(pid){
	$.get("/site_media/html/upload_basket_asset.html?" + Math.random(), function(data){
		$('#dialog').html(data);
		$('#dialog').dialog("destroy");
		$('#dialog').dialog( {
			title : 'Upload Asset',
			width : 500,
			draggable : false,
			resizable : false,
			modal : true,
			buttons : {
				Save : function(){
					var type = $("#type").val();
					if (type == "upload" && $("#new_asset_file").val().length == 0){
						alert('please chose a file');
						$("#new_asset_file").focus();
						return false;
					} else if (type == "youtube" && $("#youtube_url").val().length == 0){
						alert('please enter youtube video url');
						$("#youtube_url").focus();
						return false;
					}
					
					if ($("#new_asset_title").val().length == 0){
						alert('please write the title');
						$("#new_asset_title").focus();
						return false;
					}
					
					if ($("#moods").val() == -1){
						alert('please select the moods');
						$("#moods").focus();
						return false;
					}
					
					$("form[id=upload_asset]").attr('action','/project/basket/new/' + pid + "/").submit();
				}, Close : function() {
					$(this).dialog("close");
					$("#dialog").html("");
				}
			}
		});
	});
}

/** show or hide the basket asset. */
function show_basket(input, bid){
	if($(input).attr("checked")){
		$.get("/project/basket/show/" + bid + "/", function(){});
	} else {
		$.get("/project/basket/hide/" + bid + "/", function(){});
	}
}

/** delete the basket asset*/
function delete_basket(bid){
	if (confirm("Delete this?")){
		$.get("/project/basket/delete/" + bid + "/", function(){
			window.location.reload();
		});
	}
}

function play_youtube(bid, y_url, edit){
	var url = "/project/basket/view/" + bid + "/";
	if (edit){
		url = "/project/basket/view_form/" + bid + "/";
	}
	$.get(url , function(data){
		$('#dialog').html(data);
		$('#dialog').dialog("destroy");
		$('#dialog').dialog( {
			title : 'Play Video',
			width : 900,
			height : 500,
			draggable : false,
			resizable : false,
			modal : true,
			buttons : {
				Save : function(){
					$.post("/project/basket/edit/" + bid + "/", $("form[id=edit_basket]").serialize(), function(){
						window.location.reload();
					});
				}, Close : function() {
					$(this).dialog("close");
					$("#dialog").html("");
				}
			}
		});
		var i_html = '<object width="600" height="330"> ' +
			'<param name="movie" value="http://www.youtube.com/v/'+
			y_url +'&amp;hl=zh_CN&amp;fs=1"></param>' +
			'<param name="allowFullScreen" value="true"></param>' +
			'<param name="allowscriptaccess" value="always"></param>' +
			'<embed src="http://www.youtube.com/v/' + y_url +
			'&amp;hl=zh_CN&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="600" height="330"></embed></object>';

		$("#player").html(i_html);
//		$("#player").html("<img src='/" + image + "' width='400px' />");
	});	
}

/** in project edit page, play the image also edit the basket title/comment */
function play_image(bid, image, edit){
	var url = "/project/basket/view/" + bid + "/";
	if (edit){
		url = "/project/basket/view_form/" + bid + "/";
	}
	$.get(url , function(data){
		$('#dialog').html(data);
		$('#dialog').dialog("destroy");
		$('#dialog').dialog( {
			title : 'Play Video',
			width : 700,
			height : 420,
			draggable : false,
			resizable : false,
			modal : true,
			buttons : {
				Save : function(){
					$.post("/project/basket/edit/" + bid + "/", $("form[id=edit_basket]").serialize(), function(){
						window.location.reload();
					});
				}, Close : function() {
					$(this).dialog("close");
					$("#dialog").html("");
				}
			}
		});
		$("#player").html("<img src='/" + image + "' width='400px' />");
	});	
}

/** in project edit page, play the video also edit the basket title/comment */
function play_video(bid, video, edit){
	var url = "/project/basket/view/" + bid + "/";
	if (edit){
		url = "/project/basket/view_form/" + bid + "/";
	}
	$.get(url, function(data){
		$('#dialog').html(data);
		$('#dialog').dialog("destroy");
		$('#dialog').dialog( {
			title : 'Play Video',
			width : 700,
			height : 420,
			draggable : false,
			resizable : false,
			modal : true,
			buttons : {
				Save : function(){
					$.post("/project/basket/edit/" + bid + "/", $("form[id=edit_basket]").serialize(), function(){
						window.location.reload();
					});
				}, Close : function() {
					$(this).dialog("close");
					$("#dialog").html("");
				}
			}
		});
		
		var player = flowplayer("player", "/site_media/flowplayer-3.2.2.swf", {
			clip: {
				url: '/' + video,
				provider: 'h264streaming',
				autoPlay:true,
				autoBuffering:true,
				scaling: "scale"
			},
			plugins: {
				h264streaming: {
					url: '/site_media/flowplayer.pseudostreaming-3.2.2.swf'
				},
				controls: {
					url: '/site_media/flowplayer.controls-3.2.1.swf',
					play:true,
					stop:true,
					scrubber: true
				}
			}
		});	
	});	
}

function add_project_client(project_id){
	var html = '<form action="/project/' + project_id + '/add_client_email/" method="post" onsubmit="return check_email(this);">\
	<table>\
	<tr>\
		<td>Name:<br /><input name="client_name" type="text" size="20" /> </td>\
		<td>Title:<br /><input name="client_title" type="text" size="15" /></td>\
	</tr>\
	<tr>\
		<td>E-mail:<br /><input name="client_email" type="text" size="20" /></td>\
		<td align="right"><br><input type="submit" value="Add" /></td>\
	</tr>\
	<table>\
	</form>';
	$('#dialog').html(html).dialog( {
		title : 'Add Email Client',
		width : 600,
		height : 400,
		draggable : false,
		resizable : false,
		modal : true, 
		close : function(){
		}
	});
}



function check_youtube(youtube_form){
	if (youtube_form.new_youtube.value==""){
		alert("Please input the youtube URL!");
		return false;
	}
	return true;
}

function check_file(file_form){
	if (file_form.new_file.value==""){
		alert("Please select a file to upload!");
		return false;
	}
	return true;
}


function show_youtube(youtube_show_form){
	if (youtube_show_form.show_youtube.value){

		return true;
	}
	show_youtube = false
	return true;
}


function check_email(email_form){
	if ( email_form.client_email.value == ""){
		alert("Please input the client's email!");
		return false;
	}
	
	return true;
}

function play_youtube1(youtube){
	$("#player").html('').css('display','none');
	i_html = '<object width="600" height="330"> ' +
		'<param name="movie" value="http://www.youtube.com/v/'+
		youtube +'&amp;hl=zh_CN&amp;fs=1"></param>' +
		'<param name="allowFullScreen" value="true"></param>' +
		'<param name="allowscriptaccess" value="always"></param>' +
		'<embed src="http://www.youtube.com/v/' + youtube +
		'&amp;hl=zh_CN&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="600" height="330"></embed></object>';

	document.getElementById('youtube').innerHTML =  i_html;
}

function safe_url(pid){
	$.get("/project/" + pid +"/safe_url/", function(data){
		$('#dialog').html(data).dialog( {
			title : 'Safe Url for this project',
			width : 700,
			height: 300,
			draggable : false,
			resizable : false,
			modal : true,
			buttons : {
				Close : function() {
					$(this).dialog("close");
					$("#dialog").html("");
				}
			}
		});
	});
}

function project_desc_height(){
	var h = $("#project_desc").height();
	if (h > 200){
		$("#project_desc").height(200).css("border", "1px solid gray");
	}
}
